package tests;


import static org.junit.Assert.*;

import javax.vecmath.*;

import math.*;

import org.junit.After;
import org.junit.Before;

public class TestSpherical {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}
	
	@org.junit.Test
	public void testConstruct() {
		double z = Math.PI/4;
		double y = Math.PI/3;
		Spherical s = new Spherical(z, y);
		
		assertEquals(z, s.getZ(), Precision.ERROR_MARGIN);
		assertEquals(y, s.getY(), Precision.ERROR_MARGIN);
	}
	
	@org.junit.Test
	public void testZRange() {
		Spherical s = new Spherical();
		s.setZ(Math.PI*2);
		assertEquals(Spherical.MAX_Z_ANGLE, s.getZ(), Precision.ERROR_MARGIN);
		s.setZ(-Math.PI*2);
		assertEquals(Spherical.MIN_Z_ANGLE, s.getZ(), Precision.ERROR_MARGIN);
	}
	
	@org.junit.Test
	public void testGetVector() {
		Spherical s = new Spherical();
		Vector3d v = new Vector3d(1, 0, 0);
		assertTrue(v.epsilonEquals(s.getVector(), Precision.ERROR_MARGIN));
	}
	
	@org.junit.Test
	public void testGetVectorUp() {
		Spherical s = new Spherical(Math.PI/2, 0);
		Vector3d v = new Vector3d(0, 1, 0);
		assertTrue(v.epsilonEquals(s.getVector(), Precision.ERROR_MARGIN));
	}
	
	@org.junit.Test
	public void testGetVectorForward() {
		Spherical s = new Spherical(0, -Math.PI/2);
		Vector3d v = new Vector3d(0, 0, 1);
		assertTrue(v.epsilonEquals(s.getVector(), Precision.ERROR_MARGIN));		
	}
	
	@org.junit.Test
	public void testGetVectorAngle() {
		double z = Math.PI/4;
		double y = -Math.PI/4;
		Spherical s = new Spherical(z, y);
		Vector3d v = s.getVector();
		assertEquals(Math.cos(z) * Math.cos(-y), v.x, Precision.ERROR_MARGIN);
		assertEquals(Math.sin(z), v.y, Precision.ERROR_MARGIN);
		assertEquals(Math.cos(z) * Math.sin(-y), v.z, Precision.ERROR_MARGIN);
	}
	
	@org.junit.Test
	public void testSetVector() {
		Spherical s = new Spherical();
		Vector3d v = new Vector3d(0, 1, 0);
		s.setVector(v);
		assertEquals(Math.PI/2, s.getZ(), Precision.ERROR_MARGIN);
		assertEquals(0, s.getY(), Precision.ERROR_MARGIN);	
	}
	
	@org.junit.Test
	public void testVectorToVector() {
		Spherical s = new Spherical();
		Vector3d v = new Vector3d(1, -1, 0.5);
		s.setVector(v);
		v.normalize();
		
		Vector3d n = s.getVector();
		assertEquals(v.x, n.x, Precision.ERROR_MARGIN);
		assertEquals(v.y, n.y, Precision.ERROR_MARGIN);
		assertEquals(v.z, n.z, Precision.ERROR_MARGIN);
	}

}
